//
//  QRCodeLoginIPadViewController.m
//  WeChatQQStaticLibDemo
//
//  Created by mac on 17/2/14.
//  Copyright © 2017年 srulos. All rights reserved.
//

#import "QRCodeLoginIPadViewController.h"
#import "WeChatQQStaticLibHeader.h"

@interface QRCodeLoginIPadViewController ()
{
    NSString * m_uuid;
    BOOL m_isQuite;
}
@end

@implementation QRCodeLoginIPadViewController
@synthesize m_color;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"微信登陆";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(backBtnAction)];
    
    UINavigationBar *bar = [UINavigationBar appearance];
    bar.barTintColor = m_color;
    bar.tintColor = [UIColor whiteColor];
    [bar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    
    [self loadQRCode];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    m_isQuite = YES;
}

- (void)backBtnAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)loadQRCode
{
    NSLog(@"二维码加载中...");
    [[AsyncWeChatSimulationService stander]asyncGetUUIDAndQRCode:^(NSString *uuid, NSData *data) {
        m_uuid = uuid;
        
        if (data != nil) {
            UIImage * qrcode = [UIImage imageWithData:data];
            [self.m_imgQrcode setImage:qrcode];
            
            [self checkQRCode];
            
            NSLog(@"加载二维码成功！");
        }
        else{
            NSLog(@"加载二维码失败！");
        }
    }];
}

-(void)checkQRCode
{
    if (m_isQuite) {
        return;
    }
    
    [[AsyncWeChatSimulationService stander]asyncCheckQRCode:m_uuid loginSuccessBlock:^{
        NSLog(@"登录成功！");
        [self dismissViewControllerAnimated:YES completion:nil];
    } setAvatarImage:^(UIImage *headImage) {
        if (headImage != nil) {
            self.m_imgQrcode.hidden = YES;
            self.m_imgAvatar.hidden = NO;
            [self.m_imgAvatar setImage:headImage];
            self.m_label_tips1.hidden = YES;
            self.m_label_tips2.hidden = NO;
        }
        [self checkQRCode];
    } continueBlock:^{
        [self checkQRCode];
        NSLog(@"请扫描!!!");
    }];
}

@end
